package com.meituan.catering.management.shop.biz.model.converter;

import com.meituan.catering.management.common.model.biz.UserContextBO;
import com.meituan.catering.management.common.model.enumeration.BusinessTypeEnum;
import com.meituan.catering.management.common.model.enumeration.DescribableEnum;
import com.meituan.catering.management.common.validation.ValidationHelper;
import com.meituan.catering.management.shop.api.http.model.enumeration.ManagementTypeEnum;
import com.meituan.catering.management.shop.api.http.model.request.CreateShopHttpRequest;
import com.meituan.catering.management.shop.api.http.model.request.SaveShopHttpRequest;
import com.meituan.catering.management.shop.api.http.model.request.UpdateShopHttpRequest;
import com.meituan.catering.management.shop.biz.model.ShopBO;
import com.meituan.catering.management.shop.dao.model.ShopDO;

import java.util.Date;

/**
 * 从其他数据模型向门店BO的转换器
 */
public abstract class ShopBOConverter {

    public static ShopBO fromHttpRequest(UserContextBO userContext, CreateShopHttpRequest createShopHttpRequest) {
        if (userContext != null && createShopHttpRequest != null) {
            ShopBO shopBO = fromSaveShopHttpRequest(userContext, new ShopBO(), createShopHttpRequest);
            shopBO.getAuditing().setCreatedBy(userContext.getUserId());
            shopBO.getAuditing().setCreatedAt(new Date());
            //创建默认启动
            shopBO.setEnabled(true);
            return shopBO;
        } else {
            return null;
        }
    }

    public static ShopBO fromHttpRequest(UserContextBO userContext, ShopBO existingShopBO, UpdateShopHttpRequest updateShopHttpRequest) {
        if (userContext != null && existingShopBO != null && updateShopHttpRequest != null) {
            existingShopBO = fromSaveShopHttpRequest(userContext, existingShopBO, updateShopHttpRequest);
            existingShopBO.getAuditing().setLastModifiedBy(userContext.getUserId());
            existingShopBO.getAuditing().setLastModifiedAt(new Date());
            existingShopBO.setVersion(updateShopHttpRequest.getVersion());
            return existingShopBO;
        } else {
            return null;
        }
    }

    private static ShopBO fromSaveShopHttpRequest(UserContextBO userContext, ShopBO shopBO, SaveShopHttpRequest saveShopHttpRequest) {
        if (userContext != null && shopBO != null && saveShopHttpRequest != null) {
            ValidationHelper.validate(userContext);
            shopBO.setTenantId(userContext.getTenantId());
            shopBO.setName(saveShopHttpRequest.getName());
            shopBO.setBusinessType(DescribableEnum.getByCode(BusinessTypeEnum.class, saveShopHttpRequest.getBusinessType().getCode()));
            shopBO.getContact().setTelephone(saveShopHttpRequest.getContact().getTelephone());
            shopBO.getContact().setCellphone(saveShopHttpRequest.getContact().getCellphone());
            shopBO.getContact().setName(saveShopHttpRequest.getContact().getName());
            shopBO.getContact().setAddress(saveShopHttpRequest.getContact().getAddress());
            shopBO.setManagementType(DescribableEnum.getByCode(ManagementTypeEnum.class, saveShopHttpRequest.getManagementType().getCode()));
            shopBO.setOpenTime(saveShopHttpRequest.getOpeningHours().getOpenTime());
            shopBO.setCloseTime(saveShopHttpRequest.getOpeningHours().getCloseTime());
            shopBO.setBusinessArea(saveShopHttpRequest.getBusinessArea());
            shopBO.setComment(saveShopHttpRequest.getComment());
            return shopBO;
        } else {
            return null;
        }
    }


    public static ShopBO fromDO(ShopDO shopDO) {
        if (shopDO == null) {
            return null;
        } else {
            ShopBO shopBO = new ShopBO();
            shopBO.setId(shopDO.getId());
            shopBO.setTenantId(shopDO.getTenantId());
            shopBO.getAuditing().setCreatedBy(shopDO.getCreatedBy());
            shopBO.getAuditing().setCreatedAt(shopDO.getCreatedAt());
            shopBO.getAuditing().setLastModifiedBy(shopDO.getLastModifiedBy());
            shopBO.getAuditing().setLastModifiedAt(shopDO.getLastModifiedAt());
            shopBO.setBusinessNo(shopDO.getBusinessNo());
            shopBO.setName(shopDO.getName());
            /*
             * 数据库拿到的为枚举名称
             */
            shopBO.setBusinessType(BusinessTypeEnum.valueOf(shopDO.getBusinessType()));
            shopBO.getContact().setTelephone(shopDO.getContactTelephone());
            shopBO.getContact().setCellphone(shopDO.getContactCellphone());
            shopBO.getContact().setName(shopDO.getContactName());
            shopBO.getContact().setAddress(shopDO.getContactAddress());
            /*
             * 数据库拿到的为枚举名称
             */
            shopBO.setManagementType(ManagementTypeEnum.valueOf(shopDO.getManagementType()));
            shopBO.setOpenTime(shopDO.getOpeningHoursOpenTime());
            shopBO.setCloseTime(shopDO.getOpeningHoursCloseTime());
            shopBO.setBusinessArea(shopDO.getBusinessArea());
            shopBO.setComment(shopDO.getComment());
            String enabled = shopDO.getEnabled();
            /**
             * BO 状态为boolean
             */
            shopBO.setEnabled(!"0".equals(enabled));
            shopBO.setVersion(shopDO.getVersion());
            return shopBO;
        }

    }
}